//
//  HHTestViewController.m
//  Flower
//
//  Created by 花花 on 2016/12/7.
//  Copyright © 2016年 花花. All rights reserved.
//

#import "HHTestViewController.h"
#import "HHTextField.h"


@interface HHTestViewController ()<UITextFieldDelegate>{
BOOL isHaveDian;
}
@property(nonatomic, strong) UIButton *hongbaobtn;
@property(nonatomic, strong) UITextField *text2;

@end

@implementation HHTestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor =[UIColor colorWithHexString:@"cccccc"];
        HHTextField *text =[[HHTextField alloc]initWithFrame:CGRectMake(10, 100, screen_width-20, 30) lowerlefttitle:@"."];
        text.placeholder = @"请输入。。。";
        text.borderStyle = UITextBorderStyleRoundedRect;
    
    HHTextField *text1 =[[HHTextField alloc]initWithFrame:CGRectMake(10, 150, screen_width-20, 30) lowerlefttitle:@"X"];
    text1.placeholder = @"请输入。。。";
    text1.borderStyle = UITextBorderStyleRoundedRect;
    
    
    
    UILabel *label =[[UILabel alloc]initWithFrame:CGRectMake(10, 200, screen_width-20, 50)];
    label.text = @"只能输入0.XX 限制只能9位数字 并且.只能输入一次 同时不能.开头";
    label.numberOfLines = 0;
    [self.view addSubview:label];
    self.text2 =[[UITextField alloc]initWithFrame:CGRectMake(10, 250, screen_width-20, 30)];
    self.text2.borderStyle =  UITextBorderStyleRoundedRect;
   self. text2.keyboardType = UIKeyboardTypeDecimalPad;
    self.text2.placeholder = @"请输入...";
   self. text2.delegate = self;
    [self.view addSubview:text];
    [self.view addSubview:text1];
    
    self.hongbaobtn = [UIButton buttontitle:@"发红包" frame:CGRectMake(10, 300, screen_width-20, 40) target:self action:@selector(hongbaoAction)];
     [self.hongbaobtn setBackgroundColor:[UIColor colorWithHexString:@"FA5064"]];
    
    [self.view addSubview:self.hongbaobtn];
    [self.view addSubview:self.text2];
}
-(void)hongbaoAction{

    NSLog(@"%s",__func__);

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
   
    
  
    
    
    if (self.text2 == textField) {
        
      
        NSInteger existedLength = textField.text.length;
        NSInteger selectedLength = range.length;
        NSInteger replaceLength = string.length;
        
        if (existedLength - selectedLength + replaceLength > 9) {
            return NO;
        }
        
        if ([textField.text rangeOfString:@"."].location == NSNotFound) {
            isHaveDian = NO;
        }else if(existedLength - selectedLength + replaceLength > 9){
            return NO;
        }
        if ([string length] > 0) {
            
            unichar single = [string characterAtIndex:0];//当前输入的字符
            if ((single >= '0' && single <= '9') || single == '.') {//数据格式正确
                
                //首字母不能为0小数点
                if([textField.text length] == 0){
                    if(single == '.') {
                        [textField.text stringByReplacingCharactersInRange:range withString:@""];
                        return NO;
                    }
                    
                }
                //输入的字符是否是小数点
                if (single == '.') {
                    if(!isHaveDian)//text中还没有小数点
                    {
                        isHaveDian = YES;
                        return YES;
                        
                    }else{
                        [textField.text stringByReplacingCharactersInRange:range withString:@""];
                        return NO;
                    }
                }else{
                    if (isHaveDian) {//存在小数点
                        
                        //判断小数点的位数
                        NSRange ran = [textField.text rangeOfString:@"."];
                        if (range.location - ran.location <= 2) {
                            return YES;
                        }else{
                            //                        [self showError:@"亲，您最多输入两位小数"];
                            return NO;
                        }
                    }else{
                        return YES;
                    }
                }
            }else{//输入的数据格式不正确
                [self showHudAutoHideString:@"亲，您输入的格式不正确" ];
                [textField.text stringByReplacingCharactersInRange:range withString:@""];
                return NO;
            }
        }
        else
        {
            return YES;
        }

    }
    
    return NO;
   }





@end
